home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 22 / AMUG_22-4.ISO / Files IV / Prog / T / TinyTools-2.sit / TinyTools / StdBuffer.h / StdBuffer.h
Encoding:
C/C++ Source or Header  |  1997-10-07  |  6.0 KB  |  202 lines  |  [TEXT/MPS ]

  1. /* File StdBuffer.h
  2.     File buffering utilities, interface.
  3.     System 6 or better requires HFS
  4.     Copyright (c) 1997 by John Montbriand.  All Rights Reserved.
  5.     Permission granted for public use.
  6.     Distribute freely in areas where the laws of copyright apply.
  7.     USE AT YOUR OWN RISK.
  8.     DO NOT DISTRIBUTE MODIFIED COPIES.
  9.     Comments/questions/postcards to the author at the address:
  10.         John Montbriand
  11.         P.O. Box. 1133
  12.         Saskatoon Saskatchewan Canada
  13.         S7K 3N2
  14.     or by email at:
  15.         tinyjohn@sk.sympatico.ca
  16.     If you would like to have:
  17.         technical support regarding this file, send a postcard.
  18.     see also:
  19.         http://www3.sk.sympatico.ca/tinyjohn
  20. */
  21.  
  22. #ifndef __STDBUFFER__
  23. #define __STDBUFFER__
  24.  
  25. #include <Types.h>
  26. #include <Files.h>
  27.  
  28. #define kBufferLen 4096
  29.  
  30. typedef struct {
  31.     short refnum;
  32.     short unused;
  33.     long filepos, count;
  34.     unsigned char databytes[kBufferLen];
  35. } OutputBuffer, *OutBufPtr;
  36.  
  37. typedef struct {
  38.     short refnum;
  39.     short unused;
  40.     long filepos, count, total;
  41.     unsigned char databytes[kBufferLen];
  42. } InputBuffer, *InBufPtr;
  43.  
  44.  
  45. /* NewOutputBuffer sets up a new output buffer for writing to a file
  46.     starting at position zero.  NewOutputBuffer calls InitOutputBuffer. */
  47. OSErr NewOutputBuffer(short refnum, OutBufPtr *outbuf);
  48.  
  49. /* OpenOutputBuffer opens the file refered to by the file specification
  50.     record and then calls NewOutputBuffer. */ 
  51. OSErr OpenOutputBuffer(FSSpec *spec, OutBufPtr *outbuf);
  52.  
  53. /* CloseOutputBuffer closes the file associated with the input buffer and then
  54.     calls DisposeInputBuffer.   You should call FlushOutputBuffer
  55.     before calling DisposeOutputBuffer or CloseOutputBuffer to ensure 
  56.     remaining bytes in the buffer are saved to disk. */
  57. void CloseOutputBuffer(OutBufPtr outbuf);
  58.  
  59. /* InitOutputBuffer sets up a the fields in the output buffer record for writing
  60.     to a file from position zero.  you can call this routine to initialize a output
  61.     buffer record if you have allocated the storage yourself. */
  62. OSErr InitOutputBuffer(short refnum, OutBufPtr obp);
  63.  
  64. /* FlushOutputBuffer writes all remaining bytes in the buffer to the
  65.     file.  This routine is called automatically by DisposeOutputBuffer,
  66.     StashOutputBuffer, and TransferBytes whenever the buffer becomes
  67.     full and more space is needed to store bytes in the buffer.  You can call
  68.     it yourself at any time, if the buffer is empty, it does nothing. */
  69. OSErr FlushOutputBuffer(OutBufPtr outbuf);
  70.  
  71. /* DisposeOutputBuffer calls DisposePtr.  You should call FlushOutputBuffer
  72.     before calling DisposeOutputBuffer or CloseOutputBuffer to ensure 
  73.     remaining bytes in the buffer are saved to disk. */
  74. void DisposeOutputBuffer(OutBufPtr outbuf);
  75.  
  76. /* StashOutputBuffer stores count bytes pointed to by buffer into the
  77.     output buffer calling FlushOutputBuffer as needed. */
  78. OSErr StashOutputBuffer(OutBufPtr outbuf, void* buffer, long count);
  79.  
  80. /* OutputBufferPos returns the current file position for the output buffer */
  81. long OutputBufferPos(OutBufPtr outbuf);
  82.  
  83.  
  84. /* NewInputBuffer allocates a new input buffer and sets it up for reading
  85.     from the current file position. no bytes are actually read until the first
  86.     call to FetchInputBuffer.  You should only access the file through the
  87.     FetchInputBuffer and SetInputBufferPos until DisposeInputBuffer
  88.     is called. NewInputBuffer calls NetPtr and then it calls InitInputBuffer. */
  89. OSErr NewInputBuffer(short refnum, InBufPtr *inbuf);
  90.  
  91. /* OpenInputBuffer opens the file refered to by the file specification
  92.     record and then calls NewInputBuffer. */ 
  93. OSErr OpenInputBuffer(FSSpec *spec, InBufPtr *inbuf);
  94.  
  95. /* InitInputBuffer initializes the input buffer record for input from the
  96.     file referenced by refnum at the current file position.  This call can
  97.     be used to initialize an input buffer if you are doing your own storage
  98.     managment as it only sets up the fields in the input buffer record */
  99. OSErr InitInputBuffer(short refnum, InBufPtr ibp);
  100.  
  101. /* DisposeInputBuffer disposes of the input buffer.  This call is equivalent to
  102.     calling DisposePtr. */
  103. void DisposeInputBuffer(InBufPtr inbuf);
  104.  
  105. /* CloseInputBuffer closes the file associated with the input buffer and then
  106.     calls DisposeInputBuffer. */
  107. void CloseInputBuffer(InBufPtr inbuf);
  108.  
  109. /* FetchInputBuffer reads count bytes from the input buffer into the memory
  110.     location refered to by buffer */
  111. OSErr FetchInputBuffer(InBufPtr inbuf, void* buffer, long count);
  112.  
  113. /* SetInputBufferPos sets the file position where FetchInputBuffer
  114.     will begin retrieving bytes from on it's next call */
  115. OSErr SetInputBufferPos(InBufPtr inbuf, long newpos);
  116.  
  117. /* InputBufferFileSize returns the number of bytes stored in the
  118.     associated file. */
  119. OSErr InputBufferFileSize(InBufPtr inbuf, long *bytecount);
  120.  
  121.  
  122. /* TransferBytes transfers count bytes from the input buffer src
  123.     to the output buffer dst. */
  124. OSErr TransferBytes(InBufPtr src, OutBufPtr dst, long count);
  125.  
  126. #endif
  127.  
  128. /* timing tests for different buffer sizes
  129.  
  130.  
  131. 9k:
  132. 14@00 = '0123456789ABCD'
  133. 14@07 = '789ABCDEFGHIJK'
  134. 13 seconds to transfer 800000 bytes
  135.  
  136. 10k:
  137. 14@00 = '0123456789ABCD'
  138. 14@07 = '789ABCDEFGHIJK'
  139. 12 seconds to transfer 800000 bytes
  140.  
  141. 12k:
  142. 14@00 = '0123456789ABCD'
  143. 14@07 = '789ABCDEFGHIJK'
  144. 12 seconds to transfer 800000 bytes
  145.  
  146. 13k:
  147. 14@00 = '0123456789ABCD'
  148. 14@07 = '789ABCDEFGHIJK'
  149. 12 seconds to transfer 800000 bytes
  150.  
  151. 14k:
  152. 14@00 = '0123456789ABCD'
  153. 14@07 = '789ABCDEFGHIJK'
  154. 12 seconds to transfer 800000 bytes
  155.  
  156. 15k:
  157. 14@00 = '0123456789ABCD'
  158. 14@07 = '789ABCDEFGHIJK'
  159. 12 seconds to transfer 800000 bytes
  160.  
  161. 16k:
  162. 14@00 = '0123456789ABCD'
  163. 14@07 = '789ABCDEFGHIJK'
  164. 11 seconds to transfer 800000 bytes
  165.  
  166. 32k:
  167. 14@00 = '0123456789ABCD'
  168. 14@07 = '789ABCDEFGHIJK'
  169. 8 seconds to transfer 800000 bytes
  170.  
  171. 64k:
  172. 14@00 = '0123456789ABCD'
  173. 14@07 = '789ABCDEFGHIJK'
  174. 7 seconds to transfer 800000 bytes
  175.  
  176. 512b:
  177. 14@00 = '0123456789ABCD'
  178. 14@07 = '789ABCDEFGHIJK'
  179. 23 seconds to transfer 800000 bytes
  180.  
  181. 1k:
  182. 14@00 = '0123456789ABCD'
  183. 14@07 = '789ABCDEFGHIJK'
  184. 17 seconds to transfer 800000 bytes
  185.  
  186. 2k:
  187. 14@00 = '0123456789ABCD'
  188. 14@07 = '789ABCDEFGHIJK'
  189. 13 seconds to transfer 800000 bytes
  190.  
  191. 8k:
  192. 14@00 = '0123456789ABCD'
  193. 14@07 = '789ABCDEFGHIJK'
  194. 13 seconds to transfer 800000 bytes
  195.  
  196. 4k:
  197. 14@00 = '0123456789ABCD'
  198. 14@07 = '789ABCDEFGHIJK'
  199. 13 seconds to transfer 800000 bytes
  200.  
  201. */
  202.